//
//  ModifyViewController.m
//  FiltrationSystem
//
//  Created by 123456 on 2017/3/14.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "ModifyViewController.h"
#import "UITextField+LolitaText.h"
#import "MBProgressHUD.h"
#import "DeviceDetailsModel.h"
#import "EquipmenDetailsViewModel.h"

@interface ModifyViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nickNameText;
@property (weak, nonatomic) IBOutlet UIButton *DetermineBUtton;
@property (strong, nonatomic) NSString *nickVlue; //输入上传的昵称

@end

@implementation ModifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self updateUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.nickNameText becomeFirstResponder];
    
    
    
}


-(void)updateUI{

    self.title = @"修改设备昵称";
    self.view.backgroundColor =  RGB(33, 38, 44, 1);
    self.nickNameText.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addYTYanK_textFiledEditChanged:) name:@"UITextFieldTextDidChangeNotification" object:_nickNameText];
    
    self.nickNameText.textFieldBlock = ^(id textField , NSString * str){
        UITextField * field = (UITextField *)textField;
        if (field.text.length > 8)
            field.text = [field.text substringToIndex:8];
        _nickVlue = field.text;
        
    };
    
    self.nickNameText.delegate = self;
    
    [UITextField addYTYanKTextFielStyle1:_nickNameText withString:@"请输入新的设备昵称"];
    
    self.nickNameText.enablesReturnKeyAutomatically  =NO ;
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(backAction: )];
    
    
}

- (void)addYTYanK_textFiledEditChanged:(NSNotification *)obj {
    UITextField* textfield = (UITextField*)obj.object;
    [_nickNameText YTYanKTextFiledEditChangedObj:textfield];
}


#pragma mark - TextFile
//键盘return调用方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if([self.view viewWithTag:textField.tag+1])
        [[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
    
    [self addSystem:self.DetermineBUtton];
    
    return YES;
}


- (IBAction)addSystem:(UIButton *)sender {
    
    
    //    去两端空格和回车
    self.nickVlue = [self.nickVlue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (self.nickVlue.length == 0 || self.nickNameText.text.length == 0) {
        //        请输入有效名称
        [MBProgressHUD showError:@"设备昵称不能为空"];
        return;
    }
    
    NSDictionary *parameters;
    
    parameters = @{@"imie":self.imie,@"token":[NSUD objectForKey:@"token"],@"name":self.nickNameText.text};
    

    [EquipmenDetailsViewModel requestWithUrlForDeviceName:self andParam:parameters success:^(id model) {
        
        
        [self.navigationController popViewControllerAnimated:YES];
        [NSUD setObject:self.nickNameText.text forKey:@"device_name"];

        
    } failure:^(NSString *error) {
        
        [MBProgressHUD showError:error];

        
    }];
    
    
    
}


- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event
{
    [self.view endEditing:YES];
}


-(void)backAction:(UIBarButtonItem *)item{

    [self.navigationController popViewControllerAnimated:YES];

}


@end
